package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsManagerInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsManagerInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.ManagerInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.BankManagerService;
import cc.rengu.igas.bsps.facade.request.ManagerUnbindRequest;
import cc.rengu.igas.bsps.facade.response.ManagerUnbindResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.RedisUtil;
import cc.rengu.oltp.utility.util.StringUtil;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020.06.10
 */
public class ManagerUnbindService extends BankManagerService {
    @Override
    protected Object bizBeforeCust() throws Exception {
        ManagerUnbindRequest managerUnbindRequest = new ManagerUnbindRequest();
        ConvertUtil.convertOutput(managerUnbindRequest);
        return managerUnbindRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        ManagerUnbindRequest managerUnbindRequest = (ManagerUnbindRequest) request;
        /*获取法人行号*/
        String instId = managerUnbindRequest.getHeader().getInstId();
        /*获取用户标识*/
        String userId = managerUnbindRequest.getUserId();
        /*获取拓展经理工号*/
        String managerId = managerUnbindRequest.getManagerId();
        /*必填性校验*/
        if (StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(userId) || StringUtil.isEmptyOrNull(managerId)) {
            rglog.error("必填性校验失败！");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        /*根据用户openId查询拓展经理信息*/
        BspsManagerInfoMapper bspsManagerInfoMapper = new BspsManagerInfoMapperImpl();
        ManagerInfo managerInfo = null;
        if ("APP".equals(managerUnbindRequest.getHeader().getChanlId())){
            managerInfo = bspsManagerInfoMapper.selectBspsManagerInfoById(instId,userId);
        }else {
            managerInfo = bspsManagerInfoMapper.selectBspsManagerInfoByOpenId(instId, userId);
        }
        if (null == managerInfo) {
            rglog.error("拓展经理信息查询失败！");
            throw new BizException(RespCodeEnum.GET_MANAGER_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MANAGER_INFO_ERROR.getRespDesc());
        }
        /*更新拓展经理信息*/
        managerInfo.setOpenId("OPENID");
        managerInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        managerInfo.setLastOprId(userId);
        int iRturnCode = bspsManagerInfoMapper.updateBspsManagerInfoById(managerInfo);
        if (0 != iRturnCode) {
            rglog.error("拓展经理信息更新失败！");
            throw new BizException(RespCodeEnum.UPDATE_MANAGER_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_MANAGER_INFO_ERROR.getRespDesc());
        }
        String sessionKey = managerUnbindRequest.getHeader().getSession();
        RedisUtil.onceDel(sessionKey);
        /*拼装返回报文信息*/
        BizResponse<ManagerUnbindResponse> bizResponse = new BizResponse<>();
        ManagerUnbindResponse managerUnbindResponse = new ManagerUnbindResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(managerUnbindResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        ManagerUnbindResponse managerUnbindResponse = (ManagerUnbindResponse) bizResponse.getResult();
        ConvertUtil.convertInput(managerUnbindResponse);
    }
}
